package com.gxuwz.com_property.utils;

import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Component
public class MD5EncryptUtil {

    public static String encrypt(String password) {
        try {
            /**
             * saletd盐值
             *
             * String saletd = "设置盐值";
             * */
            String saletd = "1qazxsw23edc!@#qaz";
            //String saletd = "123456";
            //String saletd = "";
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            /**
             * 原文与盐值进行拼接新的字符串
             * */
            String saletdPassword = saletd + password;
            System.out.println("saletdPassword:"+saletdPassword);
            byte[] bytes = md5.digest(saletdPassword.getBytes(StandardCharsets.UTF_8));
            StringBuilder result = new StringBuilder();
            for (byte b : bytes) {
                String hex = Integer.toHexString(b & 0xFF);
                if (hex.length() == 1) {
                    result.append("0");
                }
                result.append(hex);
            }
            return result.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5加密错误", e);
        }
    }
}
